import os

def solve_intervals(intervals):
    """
    根据标程思路：
    1. 按照右端点升序排序
    2. 用贪心算法选择不冲突的区间
    """
    intervals.sort(key=lambda x: x[1])
    res = 0
    dot = -2000000000  # 对应 C++ 中 -2e9
    for l, r in intervals:
        if l > dot:
            res += 1
            dot = r
    return res

def process_file(in_file, out_file):
    """
    读取 in_file 文件中的测试数据，求解后将结果写入 out_file 文件中。
    """
    with open(in_file, 'r') as f:
        lines = f.readlines()
    n = int(lines[0].strip())
    intervals = []
    for line in lines[1:]:
        if line.strip():
            l, r = map(int, line.split())
            intervals.append((l, r))
    result = solve_intervals(intervals)
    with open(out_file, 'w') as f:
        f.write(str(result) + "\n")

def main():
    input_dir = r"C:\Users\ASUS\Desktop\2025-BBUCPC\2025校赛出题\C题\C_inputs"
    output_dir = r"C:\Users\ASUS\Desktop\2025-BBUCPC\2025校赛出题\C题\C_outputs"
    os.makedirs(output_dir, exist_ok=True)
    num_cases = 20
    for i in range(1, num_cases + 1):
        in_file = os.path.join(input_dir, f"{i}.in")
        out_file = os.path.join(output_dir, f"{i}.out")
        process_file(in_file, out_file)

if __name__ == "__main__":
    main()
